package com.bgs.eat.userBack.controller;

import com.bgs.eat.config.CommonResult;
import com.bgs.eat.page.PageRequest;
import com.bgs.eat.page.PageResult;
import com.bgs.eat.userBack.entity.UserBackEntity;
import com.bgs.eat.userBack.service.UserBackService;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @ClassName UserBackController
 * @Description TODO
 * @Author cjl
 * @Date 2022/10/11 11:05
 **/
@RestController
@RequestMapping("userBack")
@CrossOrigin
public class UserBackController {

    @Autowired
    UserBackService userBackService;

    /**
    * @Author cjl
    * @Description 后台登录
    * @Date  11:24
    * @Return com.bgs.eat.config.CommonResult
    * @param userBackEntity
    */
    @PostMapping("userBackDengLu")
    CommonResult userBackDengLu(@RequestBody UserBackEntity userBackEntity){
        if (!StringUtils.isNullOrEmpty(userBackEntity.getLoginName())&&!StringUtils.isNullOrEmpty(userBackEntity.getPassword())){
            CommonResult commonResult = userBackService.userBackDengLu(userBackEntity);
            return commonResult;
        } else {
            return CommonResult.error("用户名或密码不能为空");
        }
    }

    /**
    * @Author cjl
    * @Description 后台用户全查（模糊查）
    * @Date  15:50
    * @Return com.bgs.eat.page.PageResult
    * @param userName
    * @param pageRequest
    */
    @GetMapping("selectUserBackEntity")
    PageResult selectUserBackEntity(String userName, PageRequest pageRequest){
        PageResult pageResult =userBackService.selectUserBackEntity(userName,pageRequest);
        return pageResult;
    }

    /**
    * @Author cjl
    * @Description 后台用户添加或修改
    * @Date  15:54
    * @Return com.bgs.eat.config.CommonResult
    * @param userBackEntity
    */
    @PostMapping("saveOrUpdateUserBackEntity")
    CommonResult saveOrUpdateUserBackEntity(@RequestBody UserBackEntity userBackEntity){
        int i = userBackService.saveOrUpdateUserBackEntity(userBackEntity);
        if (i!=0){
            return CommonResult.ok("恭喜你，操作成功！");
        }else {
            return CommonResult.error("100","添加或修改失败!!!");
        }
    }


    /**
    * @Author cjl
    * @Description 后台用户删除
    * @Date  16:45
    * @Return java.lang.Integer
    * @param id
    */
    @DeleteMapping("deleteUserBackEntityById")
    Integer deleteUserBackEntityById(Integer id){
        return userBackService.deleteUserBackEntityById(id);
    }

    /**
    * @Author cjl
    * @Description 单查后台用户详情 （个人详情）
    * @Date  20:40
    * @Return com.bgs.eat.config.CommonResult
    * @param id
    */
    @GetMapping("selectUserBackById")
    CommonResult selectUserBackById(Integer id){
        UserBackEntity userBackEntity = userBackService.selectUserBackById(id);
        return CommonResult.ok(userBackEntity);
    }

    /**
    * @Author cjl
    * @Description 修改后台用户详情 （个人详情）
    * @Date  10:17
    * @Return com.bgs.eat.config.CommonResult
    * @param userBackEntity
    */
    @PostMapping("/updateUserBackEntityInfo")
    public CommonResult updateUserBackEntityInfo(@RequestBody UserBackEntity userBackEntity){
        int i = userBackService.updateUserBackEntityInfo(userBackEntity);
        return CommonResult.ok(i);
    }

    /**
    * @Author cjl
    * @Description 修改后台当前用户密码
    * @Date  10:27
    * @Return com.bgs.eat.config.CommonResult
    * @param map
    */
    @PostMapping("userBackUpdatePassword")
    CommonResult userBackUpdatePassword(@RequestBody Map<String,String> map){
        CommonResult commonResult = userBackService.userBackUpdatePassword(map);
        return commonResult;
    }
    /**
    * @Author cjl
    * @Description 查询省
    * @Date  14:02
    * @Return com.bgs.eat.config.CommonResult
    */
    @GetMapping("userBackEntityProvince")
    CommonResult userBackEntityProvince(){
        CommonResult commonResult = userBackService.userBackEntityProvince();
        return commonResult;
    }

    /**
    * @Author cjl
    * @Description 通过省id 查市
    * @Date  14:03
    * @Return com.bgs.eat.config.CommonResult
    * @param provinceId
    */
    @GetMapping("userBackEntityCity")
    CommonResult userBackEntityCity(Integer provinceId){
        CommonResult commonResult = userBackService.userBackEntityCity(provinceId);
        return commonResult;
    }

    /**
     * @Author cjl
     * @Description 通过省id 查县
     * @Date  14:03
     * @Return com.bgs.eat.config.CommonResult
     * @param cityId
     */
    @GetMapping("userBackEntityCounty")
    CommonResult userBackEntityCounty(Integer cityId){
        CommonResult commonResult = userBackService.userBackEntityCounty(cityId);
        return commonResult;
    }

    /**
    * @Author cjl
    * @Description 查询登录名称不能重复
    * @Date  18:34
    * @Return com.bgs.eat.config.CommonResult
    * @param loginName
    */
    @GetMapping("selectUserBackEntityByLoginName")
    CommonResult selectUserBackEntityByLoginName(String loginName){
        CommonResult commonResult = userBackService.selectUserBackEntityByLoginName(loginName);
        return commonResult;
    }

    
    /**
    * @Author cjl
    * @Description 后端用户 发送手机验证码
    * @Date 2022/11/1 13:37
    * @Return com.bgs.eat.config.CommonResult
    * @param map
    */
    @PostMapping("userBackVerificationCode")
    CommonResult userBackVerificationCode(@RequestBody Map<String,String> map){
        CommonResult commonResult = userBackService.userBackVerificationCode(map);
        return commonResult;
    }

    /**
    * @Author cjl
    * @Description 后端用户验证码登录
    * @Date 2022/11/1 13:37
    * @Return com.bgs.eat.config.CommonResult
    * @param map
    */
    @PostMapping("userBackPhoneLogin")
    CommonResult userBackPhoneLogin(@RequestBody Map<String,String> map){
        CommonResult commonResult = userBackService.userBackPhoneLogin(map);
        return commonResult;
    }

}
